package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
//import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 增加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
           Setmeal setmeal = new Setmeal();
           //System.out.println(setmealDTO);
           BeanUtils.copyProperties(setmealDTO, setmeal);
           //System.out.println(setmeal.toString());
           setmealMapper.insert(setmeal);
           Long setmealId = setmeal.getId();
           List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes();
           for(SetmealDish setmealDish:setmealDishList){
               setmealDish.setSetmealId(setmealId);
           }
           System.out.println(setmealDishList.toArray().toString());
           setmealDishMapper.insertBatch(setmealDishList);
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
           PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
           Page<SetmealVO> pages=setmealMapper.pageQuery(setmealPageQueryDTO);
           return new PageResult(pages.getTotal(),pages.getResult());
    }

    /**
     * 起售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void updateStatusById(int status, String id) {
           setmealMapper.updateStatusById( status, id);
    }
    /**
     * 根据id查询套餐
     */
    @Override
    public SetmealVO selectById(Long id) {
           SetmealVO setmealVO=setmealMapper.selectById(id);
           //获取套餐的餐品列表
           List<SetmealDish> setmealDishList=setmealDishMapper.list(id);
           setmealVO.setSetmealDishes(setmealDishList);
           return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
           Setmeal setmeal=new Setmeal();
           BeanUtils.copyProperties(setmealDTO, setmeal);
           setmealMapper.update(setmeal);
           //TODO:首先他们传进来的套餐菜品列表本身是不知道要插入哪个套餐的需要foreach赋值
           for(SetmealDish setmealDish:setmealDTO.getSetmealDishes()){
               setmealDish.setSetmealId(setmeal.getId());
           }
           //先删除再更新
           List<Long> list=new ArrayList<>();
           //arraylist增加元素是add
           list.add(setmeal.getId());
           System.out.println(list.set(0, setmeal.getId()));
           setmealDishMapper.deleteBySetmealIds(list);
           //setmealDTO.getSetmealDishes().get(0).getSetmealId();
           //批量插入更新后数据
           setmealDishMapper.insertBatch(setmealDTO.getSetmealDishes());

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
           //删除与该套餐有关联的菜单菜品表
           setmealDishMapper.deleteBySetmealIds(ids);
           //在套餐表中删除套餐
           setmealMapper.deleteByIds(ids);
    }

}
